package com.tamtamy.jtta.data.xml;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.tamtamy.jtta.data.UserStatsGenerationList;
import com.tamtamy.jtta.exception.NoResultException;
import com.tamtamy.jtta.exception.ParsingException;

public class XMLUserStatsGenerationList extends UserStatsGenerationList {

	private static final long serialVersionUID = 8069881382335818531L;

	private static final String KCLUSERSTATSGENERATION = "it.reply.tt.rest.entity.KcLUserStatsGeneration";
	
	public XMLUserStatsGenerationList(Document xmlDocument) throws NoResultException, ParsingException {
		super();
		
		NodeList userStatsGenerationNodeList = xmlDocument.getElementsByTagName(KCLUSERSTATSGENERATION);
		if (userStatsGenerationNodeList == null ||userStatsGenerationNodeList.getLength()==0){
			throw new NoResultException("No " + KCLUSERSTATSGENERATION + " element found");
		}
		for (int i = 0; i < userStatsGenerationNodeList.getLength(); i++) {
			Node userStatsGenerationNode = userStatsGenerationNodeList.item(i);
			try {
				Document userStatsGenerationDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
				userStatsGenerationDocument.appendChild(userStatsGenerationDocument.importNode(userStatsGenerationNode, true));
				this.add(new XMLUserStatsGeneration(userStatsGenerationDocument)); 
			} catch (ParserConfigurationException parserConfigurationException) {
				throw new ParsingException("Error parsing " + KCLUSERSTATSGENERATION + " element : " + parserConfigurationException.getMessage());
			}  
		}	
				
	}
}